package dk.eclipse.android.badassapp.sensor.magneticfield;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import dk.eclipse.android.badassapp.R;

/**
 * This activity explores the sensor API and services.
 * 
 * @author Ronni Kahalani (eclipse.dk)
 */
public class MagneticFieldActivity extends Activity implements
		MagneticFieldListener {
	private static final String NOT_AVAILABLE = "N/A";

	/**
	 * Tag used for logging information.
	 */
	private static final String LOG_TAG = MagneticFieldActivity.class
			.getSimpleName();

	private static Context CONTEXT;

	private TextView magneticXTextView;
	private TextView magneticYTextView;
	private TextView magneticZTextView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.sensor_magneticfield);
		setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_app);

		magneticXTextView = (TextView) findViewById(R.id.magneticX);
		magneticYTextView = (TextView) findViewById(R.id.magneticY);
		magneticZTextView = (TextView) findViewById(R.id.magneticZ);

		CONTEXT = this;

		String initValue = "0";
		if (!MagneticFieldManager.isSupported()) {
			initValue = NOT_AVAILABLE;
		}
		magneticXTextView.setText(initValue);
		magneticYTextView.setText(initValue);
		magneticZTextView.setText(initValue);
		
	}

	protected void onResume() {
		super.onResume();
		if (MagneticFieldManager.isSupported()) {
			MagneticFieldManager.startListening(this);
		}
	}

	protected void onDestroy() {
		super.onDestroy();
		if (MagneticFieldManager.isListening()) {
			MagneticFieldManager.stopListening();
		}
	}

	public static Context getContext() {
		return CONTEXT;
	}

	@Override
	public void onMagneticFieldChanged(float x, float y, float z) {
		magneticXTextView.setText(String.valueOf(x));		
		magneticYTextView.setText(String.valueOf(y));
		magneticZTextView.setText(String.valueOf(z));
	}

}